package cn.xiaozhu.Controller;

import cn.xiaozhu.service.facade.IUserService;
import com.sun.org.apache.xml.internal.resolver.readers.ExtendedXMLCatalogReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 用户操作
 *
 * @author shkstart
 * @date 2019/8/19 - 10:04
 */
@Controller
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUserService userService;

    /**
     * 进入添加用户
     *
     * @return
     */
    @RequestMapping(path = "/interRegister")
    public String register() {
        logger.debug("register");
        return "register";
    }

    /**
     * 添加新用户
     *
     * @param userName
     * @param password
     * @param email
     * @param phoneNumber
     * @return
     */
    @RequestMapping(path = "/register", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> addUser(@RequestParam("userName") String userName,
                                       String password, String email, String phoneNumber) throws Exception{
        Random random = new Random();
        logger.debug("register");
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(userName)) {
            logger.debug("false");
        }
        if (random.nextInt(10) % 2 == 0) {

            userService.addUser(userName, password, email, phoneNumber);

            result.put("flag", "更新成功");
        } else {
            result.put("msg", "服务器忙");
        }
        return result;
    }

    /**
     * 根据用户Id删除用户
     *
     * @param Id
     * @return
     */
    @RequestMapping(path = "/interDelete", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> delete(Integer Id) {

        Random random = new Random();
        logger.debug("del");
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(Id)) {
            logger.debug("false");
        } else {
            userService.deleteUser(Id);
        }
        result.put("msg", "服务器忙");
        result.put("flag", random.nextInt(10) % 2 == 0 ? true : false);
        return result;
    }

    /**
     * 进入更新（通过userService.selectUser(Id)方法将旧值带到更新页面中进行渲染）
     *
     * @return
     */
    @RequestMapping(path = "/interUpdate")
    public String update(@RequestParam(value = "Id", required = false) Integer Id, ModelMap model) {
        model.addAttribute("user", userService.selectUser(Id));
        logger.debug("update");
        return "update";
    }

    /**
     * 更新用户信息
     *
     * @param Id
     * @param userName
     * @param password
     * @param email
     * @param phoneNumber
     * @return
     */
    @RequestMapping(path = "/update", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> update(int Id, String userName, String password, String email, String phoneNumber) {
        Random random = new Random();
        logger.debug("update");
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(Id)) {
            logger.debug("false");
        }

        if (random.nextInt(10) % 2 == 0) {
            userService.updateUser(Id, userName, password, email, phoneNumber);
            logger.debug("updatesuccess");
            result.put("flag", "更新成功");
        } else {
            logger.debug("updatefail");
            result.put("msg", "服务器忙");
        }
        return result;
    }

    /**
     * 查询返回全部用户信息（Id无效）
     *
     * @param Id
     * @param model
     * @return
     */
    @RequestMapping(path = "/query")
    public String query(@RequestParam(value = "Id", required = false) Integer Id, ModelMap model) {
        // userService.selectUser(Id);
        model.addAttribute("userList", userService.findAllUsers());
        logger.debug("query");
        return "query";

    }

    @ExceptionHandler(Exception.class)
    public String handleError() {
        return "error";
    }
}
